/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						       bitmap_layer_cache.cpp

	$Header: /game/bitmap_layer_cache.cpp $

	$NoKeywords: $

 ************************************************************************/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "bitmap_layer_cache.h"

#include "bitmap_group.h"

// ---------------------------------------------------------------------------------------
// cache to reference a single layer in a bitmap group, which is loaded from another cache
// ---------------------------------------------------------------------------------------
t_bitmap_layer_cache_data::t_bitmap_layer_cache_data( t_source_cache const& source, 
										              std::string const& name )
					: m_source( source ), m_name( name )
{
	m_index = -1;
	m_reference_count = 0;
	m_data = 0;
}

// ---------------------------------------------------------------------------------------
// cache to reference a single layer in a bitmap group, which is loaded from another cache
// ---------------------------------------------------------------------------------------
void t_bitmap_layer_cache_data::add_reference()
{
	m_reference_count++;
}

// ---------------------------------------------------------------------------------------
// cache to reference a single layer in a bitmap group, which is loaded from another cache
// ---------------------------------------------------------------------------------------
t_bitmap_layer* t_bitmap_layer_cache_data::do_get( t_progress_handler* handler )
{
	if (m_data == 0)
	{
		m_bitmaps = m_source.get( handler );
		if (m_bitmaps == 0)
			return 0;
		if (m_index < 0)
		{
			t_bitmap_group::iterator index;

			index = m_bitmaps->find_iterator( m_name );
			if (index == m_bitmaps->end())
				return 0;

			m_index = index - m_bitmaps->begin();
		}
		m_data = &(*m_bitmaps)[m_index];
	}
	return m_data;
}

// ---------------------------------------------------------------------------------------
// cache to reference a single layer in a bitmap group, which is loaded from another cache
// ---------------------------------------------------------------------------------------
void t_bitmap_layer_cache_data::remove_reference()
{
	if (--m_reference_count == 0)
	{
		m_data = 0;
		m_bitmaps = t_bitmap_group_ptr();
	}
}

